package com.snow.view;

import cn.hutool.json.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * 分页数据
 *
 * @author by 13119
 */
@Getter
@Setter
@AllArgsConstructor
public class PageData<T> {
    private Integer page;
    private Integer size;
    private Long total;
    private List<T> list;
    /**
     * 统计信息
     */
    private JSONObject statisticsInfo;

    public static <T> PageData<T> of(Integer page, Integer pageSize, Long total, List<T> list) {
        return new PageData<>(page, pageSize, total, list, null);
    }

    public static <T> PageData<T> of(Integer page, Integer pageSize, Long total, List<T> list, JSONObject statisticsInfo) {
        return new PageData<>(page, pageSize, total, list, statisticsInfo);
    }

    public static <T> PageData<T> of(Page<T> pageData) {
        return new PageData<>(pageData.getPageable().getPageNumber() + 1, pageData.getPageable().getPageSize(), pageData.getTotalElements(), pageData.getContent(), null);
    }

    public static <T> PageData<T> of(Page<T> pageData, JSONObject statisticsInfo) {
        return new PageData<>(pageData.getPageable().getPageNumber() + 1, pageData.getPageable().getPageSize(), pageData.getTotalElements(), pageData.getContent(), statisticsInfo);
    }

    public PageData<?> setStatisticsInfo(String key, Object value) {
        if (this.statisticsInfo == null) {
            this.statisticsInfo = new JSONObject();
        }
        getStatisticsInfo().set(key, value);
        return this;
    }
}
